package pro.softzhang.algo.lc1100;

import pro.softzhang.algo.common.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 *
 */
public class LC1161_MaximumLevelSumOfABinaryTree {
    public static void main(String[] args) {

    }

    /**
     *
     */
    static
    class Solution {
        public int maxLevelSum(TreeNode root) {
            Queue<TreeNode> q = new LinkedList<>();
            q.add(root);
            int depth = 1, max = Integer.MIN_VALUE, answer = 0;
            while (!q.isEmpty()) {
                int size = q.size(), levelSum = 0;
                while (size-- > 0) {
                    TreeNode node = q.poll();
                    if(node.left != null) {
                        q.offer(node.left);
                    }
                    if (node.right != null) {
                        q.offer(node.right);
                    }
                    levelSum += node.val;
                }
                if (levelSum > max) {
                    max = levelSum;
                    answer = depth;
                }
                depth++;
            }
            return answer;
        }
    }
}
